import os
import time
class Writer:
    def write(self, write):
        pass
    
    def swrite(self, message):
        path = os.path.join("log", "error.log")
        try:
            logfile = file(path, "w")
        except IOError, error:
            print "Error writing to program log file " + path + "(" + error + ")"
            return
        logfile.write(message)
        logfile.close()
        
# To-do: XML output to logfile
class XMLWriter(Writer):
    def write(self, write):
        pass
    
class TextWriter(Writer):
    def write(self, write):
        path = os.path.join("log", "log.txt")
        try:
            logfile = file(path, "w")
        except IOError, error:
            print "Error writing to log file " + path + "(" + error + ")"
            return
        for a in write:
            string = time.asctime() + " - " + a.Name + ": " + a.Message + "\n"
            logfile.write(string)
        logfile.close()

class Log:
    message_log = []
    Writer = Writer()
    def __init__(self,typ):
        self.typ = typ;
        self.Writer = eval(typ + "Writer")()
        
    def add(self,message):
        self.message_log.append(message)
        self.write()
        
    def write(self):
        self.Writer.write(self.message_log)

    def swrite(self, mes):
        self.Writer.swrite(mes)
